﻿#include "attendanceface.h"
#include "ui_attendanceface.h"
#include "baidufaceapi.h"
#include "msgapi.h"
#include "common.h"
#include <QMessageBox>
#include <QDateTime>

AttendanceFace::AttendanceFace(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::AttendanceFace) {
    ui->setupUi(this);
    devCamera = NULL;
}

AttendanceFace::~AttendanceFace() {
    delete ui;
    delete devCamera;
}

void AttendanceFace::startCamera() {
    if(devCamera == NULL) {
        devCamera = new DeviceCamera(ui->camera_show);
    }
}

void AttendanceFace::stopCamera() {
    if(devCamera != NULL) {
        delete devCamera;
        devCamera = NULL;
    }
}

void AttendanceFace::on_capture_button_clicked() {

    QString currentTimestamp = QString::number(time(NULL));
    QString picPath = "./search/search_" + currentTimestamp + ".bmp";
    //拍照
    devCamera->capturePic(picPath);


    //搜索人脸库是否存在 该人脸
    UfInfo ufInfo = BaiduFaceAPI::faceSearch(picPath);
    if(ufInfo.username.isEmpty()) {
        QMessageBox::warning(this, "警告信息", "该用户未注册或者人脸识别请求失败,请先注册或者检查...");
        return;
    }

    QString currentTime = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz");
    bool result = MsgApi::getMsgApi()->insertAttendanceRecord(ufInfo.userId, ATT_FACE, picPath, currentTime);
    if(!result) {
        qDebug() << "on_capture_button_clicked send msg error";
    }
}
